<?php

require_once($CONFIG->datadir.'/lib/outputlib.php');


function labelsearch($q, $start=0) {

    global $DB, $CONFIG;

    $params = array();

    // Query
    $entriessql = "SELECT distinct e.url, e.title, e.contenttoshow FROM entrykeywords ek 
                   JOIN keywords k ON ek.keywordid = k.id 
                   JOIN entries e ON ek.entryid = e.id
                   WHERE ";
    $entriessql.= addquerywords($q, 'k.word');

    // Lang filter
    $entriessql.= addQueryLangs($params);

    // Total rows count
    // We are lucky, we have mysql cache activated :)
    $total = count($DB->selectResponses($entriessql, $params));

    // Order by density
    $entriessql.= " ORDER BY ek.density DESC ";

    // Add limit
    $entriessql.= ' LIMIT '.$start.', '.(intval($start) + $CONFIG->search_entriesperpage);

    $entries = $DB->selectResponses($entriessql, $params);


    if (!$entries) {
        return false;
    }

    $results = array();
    foreach ($entries as $key => $entry) {
        $results[$key]->url = iconv("ISO-8859-1", "UTF-8", $entry->url);
        $results[$key]->title = iconv("ISO-8859-1", "UTF-8", $entry->title);
        $results[$key]->contenttoshow = iconv("ISO-8859-1", "UTF-8", $entry->contenttoshow);
    }

    showResults($results, $total, $start);
}


function titlesearch($q, $start=0) {

    global $DB, $CONFIG;

    // Query
    $entriessql = "SELECT distinct e.url, e.title, e.contenttoshow FROM entries e WHERE";
    $entriessql.= addquerywords($q, 'title');

    // Add lang filter
    $entriessql.= addQueryLangs($params);

    // Total rows count
    // We are lucky, we have mysql cache activated :)
    $total = count($DB->selectResponses($entriessql, $params));

    // Order by 
    $entriessql.= " ORDER BY e.date DESC ";

    // Add limit
    $entriessql.= ' LIMIT '.$start.', '.(intval($start) + $CONFIG->search_entriesperpage);

    $entries = $DB->selectResponses($entriessql);


    if (!$entries) {
        return false;
    }

    $results = array();
    foreach ($entries as $key => $entry) {
        $results[$key]->url = iconv("ISO-8859-1", "UTF-8", $entry->url);
        $results[$key]->title = iconv("ISO-8859-1", "UTF-8", $entry->title);
        $results[$key]->contenttoshow = iconv("ISO-8859-1", "UTF-8", $entry->contenttoshow);
    }

    showResults($results, $total, $start);
}


function mediasearch($q, $table, $start=0) {

    global $DB, $CONFIG;

    $params = array();

    // Query
    $entriessql = "SELECT distinct e.url, e.title, m.value as contenttoshow
                   FROM entries e 
                   JOIN ".$table." m ON m.entryid = e.id 
                   WHERE ";
    $entriessql.= addquerywords($q, 'e.title');

    // Add lang filter
    $entriessql.= addQueryLangs($params);

    // Total rows count
    // We are lucky, we have mysql cache activated :)
    $total = count($DB->selectResponses($entriessql, $params));

    // Order by
    $entriessql.= " ORDER BY e.date DESC ";

    // Add limit
    $entriessql.= ' LIMIT '.$start.', '.(intval($start) + $CONFIG->search_entriesperpage);

    $entries = $DB->selectResponses($entriessql);


    if (!$entries) {
        return false;
    }

    foreach ($entries as $key => $entry) {

        $results[$key]->url = iconv("ISO-8859-1", "UTF-8", $entry->url);
        $results[$key]->title = iconv("ISO-8859-1", "UTF-8", $entry->title);
        $results[$key]->contenttoshow = iconv("ISO-8859-1", "UTF-8", $entry->contenttoshow);
    }

    showResults($results, $total, $start);
}


function addquerywords($q, $field) {

    $string = '';
    $strings = array();
    $qexploded = explode(' ', $q);
    if (!empty($qexploded)) {
        foreach ($qexploded as $q) {
            $strings[] = " ".$field." LIKE '%".$q."%' ";
        }
        $string = implode('AND', $strings);
    }

    return $string;
}


/**
 * Check session meta to see if language is filtered
 *
 * @param     array       $params      Returned by reference, is the array with query where options
 */
function addQueryLangs(&$params) {

   if (!empty($_SESSION['meta']) && $_SESSION['meta'] != '') {

        $langsql = "SELECT id FROM langs WHERE name = ?";
        $userlang = $DB->selectResponse($langsql, array($_SESSION["meta"]));
        $userlangstring = " AND langid = ? ";
        $params[] = $userlang->id;
    } else {
        $userlangstring = "";
    }

    return $userlangsstring;
}


?>
